
#include "Render.h"
#include "concurrent/ThreadManager.h"
#include "AndroidAudioRender.h"
#include "AndroidVideoRender.h"
#include "MP4Render.h"
#include "RTMPRender.h"
#include "RTSPRender.h"

void IRender::handleMessage(Message *pMsg) {
  switch (pMsg->what) {
    case MsgIDRender_Start:
      startRender();
      break;
    case MsgIDRender_Stop:
      stopRender();
      break;
    case MsgIDRender_Resume:
      resumeRender();
      break;
    case MsgIDRender_Pause:
      pauseRender();
      break;
  }
}


IRender *Render_Create(RenderParam *pRenderParam) {
  IRender *pRender = nullptr;

  switch (pRenderParam->type) {
    case RenderType_Audio:
      pRender = new AndroidAudioRender(pRenderParam);
      break;
    case RenderType_Video:
      pRender = new AndroidVideoRender(pRenderParam);
      break;
    case RenderType_MP4:
      pRender = new MP4Render(pRenderParam);
      break;
    case RenderType_RTMP:
      pRender = new RTMPRender(pRenderParam);
      break;
    case RenderType_RTSP:
      pRender = new RTSPRender(pRenderParam);
      break;
  }

  if (pRender)
    ThreadManager::Instance()->createThread(pRender);

  return pRender;
}

void Render_Destroy(IRender *pRender) {
  if (pRender && pRender->isAlive()) {
    ThreadManager::Instance()->destroyThread(pRender);
  }
}

void Render_Start(IRender *pRender) {
  if (pRender && pRender->isAlive()) {
    pRender->sendMessage(MsgIDRender_Start);
  }
}

void Render_Stop(IRender *pRender) {
  if (pRender && pRender->isAlive()) {
    pRender->sendMessage(MsgIDRender_Stop);
  }
}

void Render_Resume(IRender *pRender) {
  if (pRender && pRender->isAlive()) {
    pRender->sendMessage(MsgIDRender_Resume);
  }
}

void Render_Pause(IRender *pRender) {
  if (pRender && pRender->isAlive()) {
    pRender->sendMessage(MsgIDRender_Pause);
  }
}